package myTest;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

public class URLDecoderTest {
	private static String parseTrackUrl(String trackUrl) {
		trackUrl = trackUrl.trim().replace("%7B", "{").replace("%7D", "}")
				.replace("%7b", "{").replace("%7d", "}").replace(" ", "")
				.replace("，", ",").replace("｛", "{").replace("｝", "}")
				.replace("：", ":").replace("‘", "'").replace("’", "'")
				.replace("%27", "'").replace("%22", "\"").replace("%20", "")
				.replace("':'", "\001").replace("','", "\002")
				.replace("':", "':'").replace(",'", "','")
				.replace("\001", "':'").replace("\002", "','")
				.replace(":'}", "''}");
		if (!trackUrl.endsWith("'}") && !trackUrl.endsWith("\"}")
				&& !trackUrl.endsWith("'")) {
			trackUrl += "'}";
		}
		if (!trackUrl.endsWith("}")) {
			trackUrl += "}";
		}
		char tmpChar = trackUrl.charAt(trackUrl.length() - 2);
		if (tmpChar != 39 && tmpChar != '"') { // 倒数第二个不是单引号
			trackUrl = trackUrl.substring(0, trackUrl.length() - 1) + "'}";
		}
		// if(!trackUrl.endsWith("}")){
		// trackUrl += "}";
		// }
		// char tmpChar = trackUrl.charAt(trackUrl.length() - 2);
		// if (tmpChar != 39){ //倒数第二个不是单引号
		// trackUrl = trackUrl.substring(0, trackUrl.length() - 1)+"'}";
		// }

		return trackUrl;
	}

	/**
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		String content = "{'new_uv':'1','new_session':'1','init_refer':'http%3A%2F%2Fm.yz2.sm.cn%2Fs%3Fq%3D%E4%B8%9C%E9%A3%8E%E6%82%A6%E8%BE%BE%E8%B5%B7%E4%BA%9Ak2%E6%94%B9%E8%A3%85%26from%3Dwm838973%26safe%3D1%26snum%3D6','br58':'','coords':'_','tag_tmp':'','spm':'\\x22{keywordid}.{creative}\\x22','utm_campaign':'','utm_source':'sem_sm_m','GTID':'130375781189363541207249917','addtional_count':'0','actiontype':'m_zhuzhan','pagesize':'25','area':'1','cate':'4,29','promationinfo_count':'0','adinfo_count':'0','pagenum':'1','pagetype':'list','commoninfo_count':'957','level7':'','topinfo_count':'0','is_biz':'false','GA_pageview':'/m/bj/car/ershouche/list/'}";

		System.out.println(URLDecoder.decode(content, "UTF-8"));
		Map<String, String> paraMap = JsonUtil.fromJson(parseTrackUrl(content), Map.class);
		System.out.println(paraMap);

	}

}
